<?php
namespace app\api\controller\wanlshop;

use addons\wanlshop\library\WanlSdk\Common;
use app\common\controller\Api;
use app\index\library\wechat\MiniApp;
use think\Cache;
use think\Db;
use think\Exception;
use fast\Http;
use app\api\model\wanlshop\Order;
use app\api\model\wanlshop\Address;
use app\api\model\wanlshop\Pay;
use app\api\model\wanlshop\OrderAddress;

/**
 * WanlShop地址接口
 */
class Web extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 修改/新增地址
     *
     * @ApiSummary  (WanlShop 地址接口修改/新增地址)
     * @ApiMethod   (POST)
     *
     * @param string $user_id 用户ID
     */
    public function address()
    {
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $request = $this->request->post();
            $address = new \app\api\model\wanlshop\Address();
            $data = $request['data'];
            // 操作
            switch ($request['type']) {
                case "add":
                    $data['default'] = 1;
                    $address->data($data);
                    $address->allowField(true)->save(); // 1.1.5升级
                    $this->success('地址回调',$address);
                    break;
            }
        } else {
            $this->error(__('非法请求'));
        }
    }
    /**
     * 确认订单
     *
     * @ApiSummary  (WanlShop 订单接口确认订单)
     * @ApiMethod   (POST)
     *
     * @param string $data 商品数据
     */
    public function getOrderGoodsList()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $request = $this->request->post();
            // 订单数据
            $order = array();
            $map = array();
            // 1.0.5升级 修复客户端地址更新
            $where = !empty($request['address_id']) ? ['id' => $request['address_id']] : ['user_id' => $this->auth->id, 'default' => 1];
            // 地址
            $address = model('app\api\model\wanlshop\Address')
                ->where($where)
                ->field('id,name,mobile,province,city,district,address')
                ->find();
            // 1.1.6升级
            if(!$address){
                $this->error(__('请添加收货地址'));
            }
            // 合计
            $statis = array(
                "allnum" => 0,
                "allsub" => 0
            );
            foreach ($request['data'] as $post) {
                $redis = Common::redis();
                // 商品详情
                $goods = model('app\api\model\wanlshop\Goods')
                    ->where('id', $post['goods_id'])
                    ->field('id, shop_id, shop_category_id, activity_type, title,image,stock,freight_id,sales')
                    ->find();
                // 获取SKU
                $sku = model('app\api\model\wanlshop\GoodsSku')
                    ->where('id', $post['sku_id'])
                    ->field('id,goods_id,difference,price,market_price,stock,weigh')
                    ->find();

                // 1.1.2升级 判断是否超出库存
                $sku_key = 'goods_'.$sku['goods_id'].'_'.$sku['id'];
                if($post['number'] > $redis->llen("{$sku_key}")){
                    $this->error("系统繁忙，请稍后抢购！");
                }
                // 获取快递及价格
                $goods['freight'] = $this->freight($goods['freight_id'], $sku['weigh'], $post['number'], $address['city']);
                // 获取SKU
                $goods['sku'] = $sku;
                // 数量
                $goods['number'] = $post['number'];
                // 格式化
                if (empty($map[$goods['shop_id']])) {
                    $order[] = array(
                        "shop_id"   => $goods['shop_id'],
                        "shop_name" => $goods->shop ? $goods->shop->visible(['shopname'])['shopname']:[],
                        "products"  => [$goods],
                        "coupon" => [],
                        "freight"  => [$goods['freight']],
                        "number"    => $goods['number'],
                        "sub_price" => bcmul($sku['price'], $goods['number'], 2)
                    );
                    $map[$goods['shop_id']] = $goods;
                } else {
                    // 追加1-*
                    foreach ($order as $key => $value) {
                        if ($value['shop_id'] == $goods['shop_id']) {
                            array_push($order[$key]['products'],$goods);
                            array_push($order[$key]['freight'],$goods['freight']);
                            $order[$key]['number'] += $post['number'];
                            $order[$key]['sub_price'] = bcadd($order[$key]['sub_price'], bcmul($sku['price'], $post['number'], 2), 2);
                            break;
                        }
                    }
                }
                // 所有店铺统计
                $statis['allnum'] += $goods['number'];
            }
            // 获取运费策略-店铺循环
            foreach ($order as $key => $value) {
                $config = model('app\api\model\wanlshop\ShopConfig')
                    ->where('shop_id',$value['shop_id'])
                    ->find();
                if($config){
                    if($config['freight'] == 0){
                        // 运费叠加
                        $order[$key]['freight'] = [
                            'id' => $value['freight'][0]['id'],
                            'name' => '运费叠加',
                            'price' => array_sum(array_column($value['freight'],'price'))
                        ];
                    }else if($config['freight'] == 1){
                        // 以最低结算
                        array_multisort(array_column($value['freight'],'price'),SORT_ASC,$value['freight']);
                        $order[$key]['freight'] = [
                            'id' => $value['freight'][0]['id'],
                            'name' => $value['freight'][0]['name'],
                            'price' => $value['freight'][0]['price']
                        ];
                    }else if($config['freight'] == 2){
                        // 以最高结算
                        array_multisort(array_column($value['freight'],'price'),SORT_DESC,$value['freight']);
                        $order[$key]['freight'] = [
                            'id' => $value['freight'][0]['id'],
                            'name' => $value['freight'][0]['name'],
                            'price' => $value['freight'][0]['price']
                        ];
                    }
                    $order[$key]['order_price'] = $order[$key]['sub_price'];
                    // 2020年9月19日12:10:59 添加快递价格备份,用于还原运费
                    $order[$key]['freight_price_bak'] = $order[$key]['freight']['price'];
                    // 1.0.8升级
                    $order[$key]['sub_price'] = bcadd($order[$key]['sub_price'], $order[$key]['freight']['price'], 2);
                    $statis['allsub'] = bcadd($statis['allsub'], $order[$key]['sub_price'], 2);
                }else{
                    $this->error(__('商家未配置运费组策略，暂不支持下单'));
                }

            }
            // 传递Token
            $datalist['token'] = Common::creatToken('orderToken_'. $this->auth->id);
            // 地址
            $datalist['addressData'] = $address;
            // 订单
            $datalist['orderData']['lists'] = $order;
            $datalist['orderData']['statis'] = $statis;
            $this->success('ok', $datalist);
        } else {
            $this->error(__('非法请求'));
        }
    }
    /**
     * 获取运费模板和子类 内部方法
     * 1.1.6升级
     * @param string $id  运费ID
     * @param string $weigh  商品重量
     * @param string $number  商品数量
     * @param string $city  邮递城市
     */
    private function freight($id = null, $weigh = null, $number = 0, $city = '南山区')
    {
        // 运费模板
        $data = model('app\api\model\wanlshop\ShopFreight')->where('id', $id)->field('id,delivery,isdelivery,name,valuation')->find();
        if(!$data){
            $this->error(__('此商品运费模板不存在，暂不支持下单'));
        }
        $data['price'] = 0;
        // 是否包邮:0=自定义运费,1=卖家包邮
        if($data['isdelivery'] == 0){
            // 获取地址编码 1.1.0升级
            $list = model('app\api\model\wanlshop\ShopFreightData')
                ->where([
                    ['EXP', Db::raw('FIND_IN_SET('.model('app\common\model\Area')->get(['name' => $city])->id.', citys)')],
                    'freight_id' => $id
                ])
                ->find();
            // 查询是否存在运费模板数据
            if(!$list){
                $list = model('app\api\model\wanlshop\ShopFreightData')->get(['freight_id' => $id]);
            }

            // 计价方式:0=按件数,1=按重量,2=按体积  1.0.2升级
            if($data['valuation'] == 0){
                if($number <= $list['first']){
                    $price = $list['first_fee'];
                }else{
                    $additional = $list['additional'] > 0 ? $list['additional'] : 1; //因为要更换vue后台，临时方案，为防止客户填写0
                    $price = bcadd(bcmul(ceil(($number - $list['first']) / $additional), $list['additional_fee'], 2), $list['first_fee'], 2);
                }
            }else{
                $weigh = $weigh * $number; // 订单总重量
                if($weigh <= $list['first']){ // 如果重量小于等首重，则首重价格
                    $price = $list['first_fee'];
                }else{
                    $additional = $list['additional'] > 0 ? $list['additional'] : 1;
                    $price = bcadd(bcmul(ceil(($weigh - $list['first']) / $additional), $list['additional_fee'], 2), $list['first_fee'], 2);
                }
            }
            $data['price'] = $price;
        }
        return $data;
    }
    /**
     * 提交订单
     *
     * @ApiSummary  (WanlShop 订单接口提交订单)
     * @ApiMethod   (POST)
     *
     * @param string $data 数组
     */
    public function addOrder()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $result = false;
            $params = $this->request->post();
//            $user_id = $this->auth->id; // 用户ID
            $addressList = [];
            $goodsList = [];
            $payList = [];
            // 判断是否有地址 1.0.2升级
            if(array_key_exists('address_id',$params['order'])){
                $address_id = $params['order']['address_id']; // 地址ID
            }else{
                $this->error(__('请点击上方添加收货地址'));
            }
            $event_type = '';
            if(array_key_exists('event_type',$params['order'])){
                $event_type = $params['order']['event_type']; // 事件ID
            }
            // 判断订单是否合法 1.0.4升级
            if(array_key_exists('lists',$params['order'])){
                $lists = $params['order']['lists'];
                if(!isset($lists) && count($lists) == 0){
                    $this->error(__('订单繁忙ERR001：请返回商品详情重新提交订单'));
                }
            }else{
                $this->error(__('订单繁忙ERR002：请返回商品详情重新提交订单'));
            }
            // 查询地址
            $address = model('app\api\model\wanlshop\Address')
                ->where(['id' => $address_id])
                ->find();
            // 1.0.4升级
            if(!isset($address)){
                $this->error(__('地址异常，没有找到该地址'));
            }
            // 数据库事务操作
            Db::startTrans();
            try {
                // 遍历已店铺分类列表
                foreach ($lists as $item) {
                    // 获取店铺ID
                    $shop_id = $item['shop_id'];
                    // 随机优惠金额
                    $rand_discount = $item['discount']??0;
                    // 查询店铺配置
                    $config = model('app\api\model\wanlshop\ShopConfig')
                        ->where('shop_id', $shop_id)
                        ->find();
                    // 如果不存在，按照累计运费
                    if(!$config){
                        $config['freight'] = 0;
                    }
                    // 生成订单
                    $order = new \app\api\model\wanlshop\Order;
                    $order->freight_type = $config['freight'];
//                    $order->user_id = $user_id;
                    $order->shop_id = $shop_id;
//                    $order->order_no = $shop_id.$user_id;
                    $order->order_no = $shop_id;
                    $order->event_type = $event_type;
                    if(isset($item['remarks'])){
                        $order->remarks = $item['remarks'];
                    }
                    // 2021年3月04日 06:54:11 修改优惠券逻辑
                    $coupon = model('app\api\model\wanlshop\CouponReceive')
                        ->where(['id' => $item['coupon_id'], 'shop_id' => $shop_id])
                        ->find();
                    $order->coupon_id = $coupon ? $coupon['id'] : 0;
                    // 要补充活动ID
                    if($order->save()){
                        $priceAll = 0; // 总价格
                        $numberAll = 0; // 总数量
                        $freightALL = [];
                        $shopGoodsAll = [];
                        // 计算订单价格
                        foreach ($item['products'] as $data){
                            $redis = Common::redis();
                            // 查询商品 1.2.0升级
                            $goods = model('app\api\model\wanlshop\Goods')
                                ->where(['id' => $data['goods_id'], 'status' => 'normal'])
                                ->find();
                            // 获取sku
                            $sku = model('app\api\model\wanlshop\GoodsSku')->get($data['sku_id']);
                            // 1.1.2升级
                            $sku_key = 'goods_'.$sku['goods_id'].'_'.$sku['id'];
                            // 1.1.0升级
                            if(!$goods) throw new Exception("对不起当前商品不存在或已下架");
                            // 效验shop_id是否正确 1.1.2升级
                            if($goods['shop_id'] != $shop_id) throw new Exception("网络异常SHOPID错误！");
                            // 1.1.2升级 提交订单判断库存
                            if($sku['stock'] <= 0){
                                throw new Exception("商品被抢光了");
                            }else if($sku['stock'] < $data['number']){
                                throw new Exception("库存不足");
                            }
                            // 库存计算方式:porder=下单减库存,payment=付款减库存
                            if($goods['stock'] == 'porder'){
                                // 1.1.2升级
                                if($data['number'] > $redis->llen("{$sku_key}")){
                                    throw new Exception("系统繁忙，请稍后抢购！");
                                }else{
                                    for ($i = 0; $i < $data['number']; $i ++) {
                                        $redis->rpop("{$sku_key}");
                                    }
                                    $sku->setDec('stock', $data['number']); // 1.0.3升级
                                }
                            }
                            // 生成运费
                            $freight = $this->freight($goods['freight_id'], $sku['weigh'], $data['number'], $address['city']);
                            // 商品列表 actual_payment
                            $shopGoodsAll[] = [
                                'order_id' => $order->id, // 获取自增ID
                                'goods_id' => $goods['id'],
                                'shop_id' => $shop_id,
                                'title' => $goods['title'],
                                'image' => $goods['image'],
                                'goods_sku_sn' => $sku['sn'],
                                'goods_sku_id' => $sku['id'],
                                'difference' => join(',', $sku['difference']),
                                'market_price' => $sku['market_price'], // 市场价
                                'price' => $sku['price'], // 原价
                                'freight_price' => $freight['price'], //快递价格
                                'discount_price' => 0, // 优惠金额
                                'actual_payment' => bcmul($sku['price'], $data['number'], 2), // 1.0.6修复 实际支付，因为要和总价进行计算
                                'number' => $data['number']
                            ];
                            $freightALL[] = $freight;
                            $priceAll = bcadd($priceAll, bcmul($sku['price'], $data['number'], 2), 2); // 计算价格
                            $numberAll += $data['number']; // 计算数量
                        }
                        // 计算运费叠加方案
                        if($config['freight'] == 0){
                            // 运费叠加
                            $freight = [
                                'id' => $freightALL[0]['id'],
                                'name' => '合并运费',
                                'price' => array_sum(array_column($freightALL,'price'))
                            ];
                        }else if($config['freight'] == 1){ // 以最低结算
                            array_multisort(array_column($freightALL,'price'),SORT_ASC,$freightALL);
                            $freight = [
                                'id' => $freightALL[0]['id'],
                                'name' => $freightALL[0]['name'],
                                'price' => $freightALL[0]['price']
                            ];
                        }else if($config['freight'] == 2){ // 以最高结算
                            array_multisort(array_column($freightALL,'price'),SORT_DESC,$freightALL);
                            $freight = [
                                'id' => $freightALL[0]['id'],
                                'name' => $freightALL[0]['name'],
                                'price' => $freightALL[0]['price']
                            ];
                        }
                        $freight_price = $freight['price'];  //快递金额
                        $price = bcadd($priceAll, $freight_price, 2); // 总价格
                        $coupon_price = 0; //优惠券金额
                        $discount_price = 0; // 优惠金额，因为后续版本涉及到活动减免，所以优惠金额要单独拎出来

                        // 如果优惠券存在
                        if($coupon) {
                            // 判断是否可用
                            if($priceAll >= $coupon['limit']){
                                // 优惠金额
                                if($coupon['type'] == 'reduction' || ($coupon['type'] == 'vip' && $coupon['usertype'] == 'reduction')){
                                    $coupon_price = $coupon['price'];
                                    //总金额 =（订单金额 - 优惠券金额）+ 运费
                                    $price = bcadd(bcsub($priceAll, $coupon_price, 2), $freight['price'], 2);
                                }
                                // 折扣金额
                                if($coupon['type'] == 'discount' || ($coupon['type'] == 'vip' && $coupon['usertype'] == 'discount')){
                                    // 排除异常折扣，还原百分之
                                    $discount = $coupon['discount'] > 10 ? bcdiv($coupon['discount'], 100, 2) : bcdiv($coupon['discount'], 10, 2);
                                    // 优惠金额 = 订单金额 - 订单金额 * 折扣
                                    $coupon_price = bcsub($priceAll, bcmul($priceAll, $discount, 2), 2);
                                    $price = bcadd(bcsub($priceAll, $coupon_price, 2), $freight['price'], 2);
                                }
                                // 免邮金额
                                if($coupon['type'] == 'shipping'){
                                    $coupon_price = $freight_price;
                                    $price = $priceAll;
                                    $freight_price = 0;
                                }
                                $discount_price = $coupon_price;

                                // 总优惠金额 1.1.3弃用
                                // $paycoupon = 0;
                                // 总实际支付金额 1.1.3弃用
                                // $payment = 0;
                                foreach ($shopGoodsAll as $row) {
                                    $goods_price = bcmul($row['price'], $row['number'], 2);
                                    $goods_discount_price = round($coupon_price * ( $goods_price / $priceAll ), 2); // 优惠金额
                                    // 1.0.8升级,修复包邮
                                    $actual_payment = $coupon['type'] === 'shipping' ? $goods_price : bcsub($goods_price, $goods_discount_price, 2);
                                    //优惠价格
                                    $row['discount_price'] = $goods_discount_price;
                                    // 实际支付 1.0.9升级
                                    $row['actual_payment'] = $actual_payment <= 0 ? '0.01' : $actual_payment;
                                    $row['freight_price'] = $freight_price;
                                    // 1.0.8升级 1.1.3弃用
                                    // $paycoupon = bcadd($paycoupon, $goods_discount_price, 2);
                                    // $payment = bcadd($payment, $actual_payment, 2);
                                    $goodsList[] = $row;
                                }

                                // 更新已使用数量
                                model('app\api\model\wanlshop\Coupon')
                                    ->where(['id' => $coupon['coupon_id']])
                                    ->setInc('usenum');
                                // 修改该优惠券状态 已用
                                $coupon->allowField(true)->save(['state' => 2]);
                            }else{
                                model('app\api\model\wanlshop\Order')->destroy($order->id);
                                throw new Exception('订单金额'.$priceAll.'元，不满'.$coupon['limit'].'元');
                            }
                        }else{
                            foreach ($shopGoodsAll as $row) {
                                $goodsList[] = $row;
                            }
                        }
                        // 随机优惠
                        if ($rand_discount){
                            $price = bcsub($price,$rand_discount,2);
                        }
                        // 生成支付
                        $payList[] = [
//                            'user_id' => $user_id,
                            'shop_id' => $shop_id,
                            'order_id'  => $order->id,
                            'order_no'  => $order->order_no,
                            'pay_no' => $order->order_no,
                            'type' => 'goods', // 1.0.8升级
                            'price'  => $price <= 0 ? 0.01 : $price,  // 支付价格，系统要求至少支付一分钱
                            'order_price' => $priceAll, // 订单总金额
                            'coupon_price' => $coupon_price,  // 优惠券金额
                            'freight_price' => $freight_price, // 快递费
                            'discount_price' => $discount_price, // 优惠金额
                            'number'  => $numberAll,
                            'rand_discount_price'  => $rand_discount
                        ];
                        // 生成地址
                        $addressList[] = [
//                            'user_id' => $user_id,
                            'shop_id' => $shop_id,
                            'order_id'  => $order->id,
                            'name' => $address['name'],
                            'mobile' => $address['mobile'],
                            'address' => $address['province'].'/'.$address['city'].'/'.$address['district'].'/'.$address['address'],
                            'address_name' => $address['address_name'],
                            'location' => $address['location']
                        ];
                    }else{
                        throw new Exception('网络繁忙，创建订单失败！');
                    }
                }
                model('app\api\model\wanlshop\OrderAddress')->saveAll($addressList);
                model('app\api\model\wanlshop\OrderGoods')->saveAll($goodsList);
                $result = model('app\api\model\wanlshop\Pay')->saveAll($payList);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success('返回成功', ['list' => $result,'order_no' =>$order->order_no ]);
            } else {
                $this->error(__('订单异常，请返回重新下单'));
            }
        } else {
            $this->error(__('非法请求'));
        }
    }

    /**
     * 支付订单
     *
     * @ApiSummary  (WanlShop 支付订单)
     * @ApiMethod   (POST)
     *
     * @param string $order_id 订单ID
     * @param string $type 支付类型
     */
    public function payment()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $order_id = $this->request->post('order_id/a');
            $order_id ? $order_id : ($this->error(__('非法请求')));
            $order_type = $this->request->post('order_type');
            $type = $this->request->post('type');
            $method = $this->request->post('method');
            $code = $this->request->post('code');
            $token = $this->request->post('token');
            // 验证Token
            if($token){
                if(!Common::checkToken($token, 'orderToken_'. $this->auth->id)){
                    $this->error(__('页面安全令牌已过期！请重返此页'));
                }
            }else{
                $this->error(__('非法提交，未传入Token'));
            }
            $user_id = $this->auth->id;
            $type ? $type : ($this->error(__('未选择支付类型')));

            // 1.0.8升级 拼团
            if($order_type == 'groups'){
                $model_order = model('app\api\model\wanlshop\groups\Order');
                $model_order_goods = model('app\api\model\wanlshop\groups\OrderGoods');
                $model_goods = model('app\api\model\wanlshop\groups\Goods');
                $model_goods_sku = model('app\api\model\wanlshop\groups\GoodsSku');
            }else{
                $model_order = model('app\api\model\wanlshop\Order');
                $model_order_goods = model('app\api\model\wanlshop\OrderGoods');
                $model_goods = model('app\api\model\wanlshop\Goods');
                $model_goods_sku = model('app\api\model\wanlshop\GoodsSku');
            }

            // 判断权限
            $order = $model_order
                ->where('id', 'in', $order_id)
                ->where('user_id', $user_id)
                ->select();
            if(!$order){
                $this->error(__('没有找到任何要支付的订单'));
            }
            foreach($order as $item){
                if($item['state'] != 1){
                    $this->error(__('订单已支付，或网络繁忙'));
                }
                // 1.0.5升级 修复付款减库存
                foreach($model_order_goods->where('order_id', $item['id'])->select() as $data){
                    $redis = Common::redis();
                    // 获取sku
                    $sku = $model_goods_sku->get($data['goods_sku_id']);
                    // 1.1.2升级
                    $sku_key = ($order_type == 'groups' ? 'groups':'goods').'_'.$sku['goods_id'].'_'.$sku['id'];
                    // 查询商品
                    $goods = $model_goods
                        ->where(['id' => $data['goods_id'], 'stock' => 'payment'])
                        ->find();
                    // 库存计算方式:porder=下单减库存,payment=付款减库存 1.0.8升级
                    if($goods) {
                        // 1.1.2升级
                        if($data['number'] > $redis->llen("{$sku_key}")){
                            $this->error("系统繁忙，请稍后抢购！");
                        }else{
                            for ($i = 0; $i < $data['number']; $i ++) {
                                $redis->rpop("{$sku_key}");
                            }
                            $sku->setDec('stock', $data['number']); // 1.0.3升级
                        }
                    }
                }
            }
            // 调用支付
            $wanlPay = new WanlPay($type, $method, $code);
            $data = $wanlPay->pay($order_id, $order_type);
            if($data['code'] == 200){
                $this->success('ok', $data['data']);
            }else{
                $this->error($data['msg']);
            }
        }
        $this->error(__('非正常请求'));
    }
    // 获取小程序跳转域名
    public function getDomain()
    {
        $params = $this->request->post();
        $path = $params['path'];
        $query['order_no']   = $params['order_no'];
        $query['address_id']   = $params['address_id'];
        $config = get_addon_config('wanlshop');
        $appid = $config['mp_weixin']['appid'];
        $secret = $config['mp_weixin']['appsecret'];

        $token = MiniApp::getAccessToken($appid,$secret);
        $query = http_build_query($query);

        $data = [
            "path" => $path,
            "query" => $query,
            "expire_type" => 1,
            "expire_interval" => 1,
            "env_version"=>'develop'
        ];

        $options = [
            CURLOPT_HTTPHEADER => array('Content-type: application/json')
        ];

        $result = Http::post("https://api.weixin.qq.com/wxa/generate_urllink?access_token=$token",json_encode($data),$options);

        return $this->success('获取成功',json_decode($result,true));

    }
    public function addUserOrder(){
        $params = $this->request->post();
        $order_no = $params['order_no'];
        $user_id = $params['user_id'];
        $address_id = $params['address_id'];
        if($order_no&&$user_id&&$address_id){
            $order = Order::where('order_no',$order_no)->find();
            $order->user_id = $user_id;
            $order->save();
            $address = Address::where('id',$address_id)->find();
            $address->user_id = $user_id;
            $address->save();
            $pay = Pay::where('order_no',$order_no)->find();
            $pay->user_id = $user_id;
            $pay->save();
            $orderAddress = OrderAddress::where('order_id',$order->id)->find();
            $orderAddress->user_id = $user_id;
            $orderAddress->save();
            return $this->success('绑定成功');
        }
    }


}
